import sys
import os
import time
sys.path.append(os.path.abspath(os.path.join(
    os.path.dirname(__file__), '../bin')))
from tools import getThreadText, checkHasActivityThreadPosted
import pdb

def _main():
    activityName = ''
    dayOffset = 0
    if len(sys.argv) > 1:
        activityName = sys.argv[1]
    else:
        activityName = '早起团'

    if len(sys.argv) > 2:
        dayOffset = int(sys.argv[2])

    if len(sys.argv) > 3:
        pdb.set_trace()
    print('activityName: {}'.format(activityName))

    hasThread = checkHasActivityThreadPosted(activityName, dayOffset)
    retJsonPath = os.path.abspath(os.path.join(os.path.dirname(__file__), '../output/req.json'))
    itJsonPath = os.path.abspath(os.path.join(os.path.dirname(__file__), '../output/it.json'))

    if not hasThread:
        if True or activityName == '惜时社' or activityName == '早起团' or activityName == '沁心运动':
            text = getThreadText(activityName, dayOffset)
            if len(text) > 0:
                fo = open(retJsonPath, "w", encoding='utf-8')

                fo.write(text)
                print('{}: {}'.format(time.asctime( time.localtime(time.time()) ), text))
                fo.close()
            else:
                print('没有生成帖子内容')
        else:
            print('目前只支持 早起团 惜时社 沁心运动，请输入正确的活动名字')

        # activityName = '撸啊撸挨踢天团'
        # fo = open(itJsonPath, "w", encoding='utf-8')
        # text = getThreadText(activityName)
        # fo.write(text)
        # print('{}: {}'.format(time.asctime( time.localtime(time.time()) ), text))
        # fo.close()
    else:
        # remove it.json
        itFilePath = os.path.abspath(os.path.join(os.path.dirname(__file__), '../output/it.json'))
        if os.path.isfile(itFilePath):
            os.remove(itFilePath)

if __name__ == '__main__':
    _main()